package cn.wolfcode.p2p.business.service.impl;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.BaseAudit;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.domain.UserBankInfo;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.wolfcode.p2p.base.domain.LoginInfo;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.query.RechargeOfflineQuery;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RechargeOfflineServiceImpl implements IRechargeOfflineService {
        @Autowired
        private RechargeOfflineMapper rechargeOfflineMapper;
        @Autowired
        private IAccountService accountService;
        @Autowired
        private IAccountFlowService accountFlowService;

        @Override
        public void saveRechargeOffline(RechargeOffline rechargeOffline) {
                //      判断基本参数
                //判断是否有交易号的审核记录并且是审核通过的
                AssertUtil.instance().isFalse(rechargeOfflineMapper.checkTradeCode(rechargeOffline.getTradeCode())>0,
                        "已经使用过的交易号,请重新输入");
                //填写资料
                RechargeOffline ro = new RechargeOffline();
                ro.setTradeCode(rechargeOffline.getTradeCode());
                ro.setTradeTime(rechargeOffline.getTradeTime());
                ro.setAmount(rechargeOffline.getAmount());
                ro.setNote(rechargeOffline.getNote());
                ro.setBankInfo(rechargeOffline.getBankInfo());
                ro.setApplyTime(new Date());
                ro.setApplier(UserContext.getLoginInfo());
                rechargeOfflineMapper.insert(ro);
        }

        @Override
        public boolean checkTradeCode(String tradeCode) {
                return rechargeOfflineMapper.checkTradeCode(tradeCode)>0;
        }

        @Override
        public PageResult query(RechargeOfflineQuery qo) {
                 //先查出COunt,如果COUNT为0,返回空集合
                 int row = rechargeOfflineMapper.selectCount(qo);
                 if(row==0){
                     return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
                 }
                 List<RealAuth> list =rechargeOfflineMapper.selectForList(qo);
                 return new PageResult(list,row,qo.getCurrentPage(),qo.getPageSize());
        }

        @Override
        public void rechargeOfflineAudit(Long id, String remark, Integer state) {
                //1判断基本参数
                AssertUtil.instance().isNotNull(id,"参数错误").isNotNull(state,"参数错误!");

                //2,判断申请是否处于待审核,0
                RechargeOffline ro = rechargeOfflineMapper.selectByPrimaryKey(id);
                AssertUtil.instance().isFalse(ro.getState()!=0,"充值申请不处于待审核!");
                //3设置公共审核信息
                LoginInfo auditor = UserContext.getLoginInfo();
                ro.setAuditor(auditor);
                ro.setAuditTime(new Date());
                ro.setRemark(remark);
                ro.setState(state);
                //查询用户的账户对象
                if(state== BaseAudit.STATE_SUCCESS){
                        Account applierAccount = accountService.getByid(ro.getApplier().getId());
                        applierAccount.addUsableAmount(ro.getAmount());
                        accountService.update(applierAccount);
                        //增加流水
                        accountFlowService.createRechargeOfflineAccountFlow(ro.getAmount(),applierAccount);

                }
                update(ro);
                //4,如果成功,给用户加钱,加在可用余额里,修改申请状态为1
                //5如果失败,直接修改订单状态为2,
                //更新订单,更新用户账户
        }

        private void update(RechargeOffline ro) {
                rechargeOfflineMapper.updateByPrimaryKey(ro);
        }
}
